﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.ComponentModel;
using DienThoai.CONTROLLERS;
using DienThoai.MODELS;

namespace DienThoai
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class Login : Window
    {
        #region <<< Data members >>>

        private LoginModel.LoginEventHandler LoginDelegate;
        #endregion
        public Login()
        {
            InitializeComponent();
            HandleEvent();
        }

        private void HandleEvent()
        {
            LoginDelegate = new LoginModel.LoginEventHandler(this.UpdateView);
            LoginModel.Instance.LoginEvent += LoginDelegate;
        }

        private void UpdateView(object sender, LoginEventArgs e)
        {
            switch (e.Type)
            {
                case Consts.AccountType.StoreKeeper:
                    {
                        StoreKeeper storeKeeper = new StoreKeeper();
                        storeKeeper.UserName = e.UserName;
                        storeKeeper.Show();
                        this.Close();
                        break;
                    }
                case Consts.AccountType.Admin:
                    {
                        Manager manager = new Manager();
                        manager.UserName = e.UserName;
                        manager.Show();
                        this.Close();
                        break;
                    }
                case Consts.AccountType.Staff:
                    {
                        BillForm billForm = new BillForm();
                        billForm.UserName = e.UserName;
                        billForm.Show();
                        this.Close();
                        break;
                    }
            }
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LoginController.Instance.InvokeAuthenticate(txtTaiKhoan.Text, pwbMatKhau.Password);
            }
            catch(Exception)
            {
                MessageBox.Show("Tài khoản hoặc mật khẩu không đúng.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            
        }

        private void LoginCompleted(object sender, LoginEventArgs e)
        { 
        
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void btnLogin_Click_1(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtTaiKhoan.Focus();
        }
    }   
}
